package com.gxks.lhs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxks.lhs.entity.Admin;
import com.gxks.lhs.entity.Report;
import com.gxks.lhs.mapper.ReportMapper;
import com.gxks.lhs.service.IAdminService;
import com.gxks.lhs.service.IReportService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * <p>
 * 举报表 服务实现类
 * </p>
 *
 * @author LuoHaisong
 * @since 2023-05-18
 */
@Service
@Slf4j
public class ReportServiceImpl extends ServiceImpl<ReportMapper, Report> implements IReportService {
    @Autowired
    private IAdminService adminService;


    @Override
    public Page<Report> findAllReports(Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<Report> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(Report::getCreateTime);
        return this.page(new Page<>(pageNum, pageSize), queryWrapper);
    }

    @Override
    public Page<Report> findReport(Report report, Integer pageNum, Integer pageSize) {
        if (report == null) {
            return this.page(new Page<>(pageNum, pageSize));
        }
        QueryWrapper<Report> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().like(report.getGoodsName() != null, Report::getGoodsName, report.getGoodsName())
                .like(report.getHandlerName() != null, Report::getHandlerName, report.getHandlerName())
                .eq(report.getType() != null, Report::getType, report.getType())
                .eq(report.getStatus() != null, Report::getStatus, report.getStatus())
                .eq(report.getUserId() != null, Report::getUserId, report.getUserId())
                .orderByDesc(Report::getCreateTime);
        return this.page(new Page<>(pageNum, pageSize), queryWrapper);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean handlingReport(String id, Integer status, String adminId, String reportResult) {
        Admin admin = adminService.selectById(adminId);
        log.info("管理员{}处理了举报{}", admin.getNickname(), id);
        log.info("处理结果为{}", reportResult);
        log.info(String.valueOf(new Date()));
        UpdateWrapper<Report> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().set(Report::getStatus, status)
                .set(Report::getHandlerId, admin.getId())
                .set(Report::getHandlerName, admin.getNickname())
                .set(Report::getProcessingTime, new Date())
                .set(Report::getReportResult, reportResult)
                .eq(Report::getId, id);
        return this.update(updateWrapper);
    }

    @Override
    public Page<Report> findReportByReporterId(Integer reporterId, Integer current, Integer size) {
        return this.page(new Page<>(current, size), new QueryWrapper<Report>().lambda().eq(Report::getUserId, reporterId));
    }

    @Override
    public boolean addReport(Report report) {
        return this.save(report);
    }

    @Override
    public boolean deleteReport(Integer id) {
        return this.removeById(id);
    }

    @Override
    public boolean updateReport(Report report) {
        return this.updateById(report);
    }
}
